#pragma once
#include<iostream>
#include<assert.h>

namespace key
{
	class string
	{
	public:
		//构造函数
		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];

			strcpy(_str, str);
		}

		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);

		}

		//拷贝函数
		string(string& s)
		{
			string tmp(s);
			swap(tmp);
		}

		//赋值函数
		string& operator=(string s)
		{
			swap(s);
			return *this;
		}

		//析构函数
		~string()
		{
			delete[] _str;
			_size = 0;
			_capacity = 0;
		}

		//迭代器

		//可读可写的迭代器
		typedef char* iterator;
		//只可读不可写的迭代器
		typedef const char* const_iterator;

		//iterator用的begin()
		iterator begin()
		{
			return _str;
		}
		//iterator用的end()
		iterator end()
		{
			return _str + _size;
		}
		//const_iterator用的begin()
		const_iterator begin()const
		{
			return _str;

		}
		//const_iterator用的end()
		const_iterator end()const
		{
			return _str + _size;

		}
		//扩容函数
		void reserve(size_t n = 0)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);

				delete[] _str;
				_str = tmp;
				_capacity = n;

			}

		}

		//c_str()函数
		const char* c_str() const
		{
			return _str;
		}


		//尾插
		void push_back(char c)
		{
			if (_size == _capacity)
			{
				int newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}

			_str[_size] = c;
			_size++;
			//处理\0
			_str[_size] = '\0';

		}

		//append
		string& append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			strcpy(_str + _size, str);
			_size += len;
			_capacity = _size + len;

			return *this;
		}

		//+=运算符重载函数
		string& operator+=(char c)
		{
			push_back(c);
			return *this;
		}

		//+=运算符重载函数
		string& operator+=(const char* str)
		{
			append(str);
			return *this;

		}

		//[]运算符重载(只可读)
		const char operator[](size_t pos)const
		{
			assert(pos < _size);
			return _str[pos];

		}

		//[]运算符重载(可读可写)
		char operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}


		void insert(size_t pos, char c)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}


			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				end--;
			}

			_str[pos] = c;
			_size++;

		}

		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);

			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			size_t end = _size + len;
			while (end > pos)
			{
				_str[end] = _str[end - len];
				end--;
			}


			strncpy(_str + pos, str, len);
			_size += len;

		}



	private:

		size_t _size = 0;
		size_t _capacity = 0;
		char* _str = nullptr;

	};

	/*void test()
	{
		string s1("hello");
		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			std::cout << *it;
			it++;
		}
		std::cout << std::endl;

	}*/

	//void test1()
	//{
	//	string s1("hello");
	//	s1 += ' ';
	//	s1 += 'w';
	//	s1 += 'o';
	//	s1 += 'r';
	//	s1 += 'l';
	//	s1 += 'd';
	//	s1.push_back('h');
	//	s1.push_back('a');
	//	s1.append("hehehe");
	//	std::cout << s1.c_str();

	//}

	/*void test2()
	{
		string s1("hello");
		s1.insert(0, 'X');

		std::cout << s1.c_str() << std::endl;

	}*/

	void test3()
	{
		string s1("hello");
		s1.insert(0, "xxxxxx");

		std::cout << s1.c_str() << std::endl;

	}


}
